package com.vvvsss.jfinal3.service;


import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.vvvsss.jfinal3.model.Animal;

import java.util.List;

/**
 * jfinal的业务官方推荐全部写在service层中 <BR>
 * create time : 2017-05-10 09:43
 *
 * @author luoyizhu@gmail.com
 */
public class AnimalService {

    private static final Animal dao = new Animal().dao();

    public List<Animal> findAnimals(int animalInfoId, int age) {

        Kv pars = Kv.by("animalInfoId", animalInfoId).set("age", age);

        SqlPara sqlPara = dao.getSqlPara("jfinalAnimal.findAnimals", pars);

        return dao.find(sqlPara);
    }

    public Animal findOne(String name, int animalInfoId, int age) {

        Kv pars = Kv.by("animalInfoId", animalInfoId).set("age", age).set("name", name);

        SqlPara sqlPara = dao.getSqlPara("jfinalAnimal.findOne", pars);

        return dao.findFirst(sqlPara);
    }


    public Page<Animal> pageByAge(int pageNumber, int pageSize, int age) {

        Kv kv = Kv.by("age", age);

        String key = "jfinalAnimal.pageByAge";
        SqlPara sqlPara = dao.getSqlPara(key, kv);

        Page<Animal> page = dao.paginate(pageNumber, pageSize, sqlPara);

        return page;
    }
}
